import 'package:custom_widgets/custom_localizations.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

///日期选择弹出框,(需要设置国际化，GlobalCupertinoLocalizations.delegate,否则在有中文的情况下会报异常)
class DatePickerSheet extends StatelessWidget {
  /// 选中的日期(未赋值或空值时,默认选中当前日期)
  final DateTime? selectDate;

  /// 最小日期
  final DateTime? minimumDate;

  /// 最大日期
  final DateTime? maximumDate;

  /// 是否24小时制(默认true)
  final bool use24hFormat;

  /// 日期选择模式
  final CupertinoDatePickerMode? mode;

  DatePickerSheet({
    Key? key,
    this.selectDate,
    this.minimumDate,
    this.maximumDate,
    this.mode,
    this.use24hFormat = true,
  }) : super(key: key);

  /// 日期弹出
  static Future<DateTime?> show(
    BuildContext context, {
    DateTime? selectDate,
    DateTime? minimumDate,
    DateTime? maximumDate,
    bool use24hFormat = true,
    CupertinoDatePickerMode? mode,
  }) {
    return showCupertinoModalPopup(
      context: context,
      builder: (BuildContext context) {
        return DatePickerSheet(
          selectDate: selectDate,
          minimumDate: minimumDate,
          maximumDate: maximumDate,
          use24hFormat: use24hFormat,
          mode: mode,
        );
      },
    );
  }

  /// 默认语言
  final Custom_Localizations _localizations = Custom_Localizations();

  @override
  Widget build(BuildContext context) {
    /// 选中日期(默认当天)
    DateTime? date = selectDate ?? DateTime.now();

    /// 最小日期不为空时, 由于当前日期小于最小日期时会报错;
    /// 该处: 如果当前日期小于最小日期时
    /// 将当前日期设置为最小日期
    if (minimumDate != null &&
        minimumDate!.microsecondsSinceEpoch > date.microsecondsSinceEpoch) {
      date = minimumDate;
    }

    /// 最大日期不为空时, 由于当前日期大于最大日期时会报错;
    /// 该处: 如果当前日期大于最小日期时
    /// 将当前日期设置为最大日期
    if (maximumDate != null &&
        maximumDate!.microsecondsSinceEpoch < date!.microsecondsSinceEpoch) {
      date = maximumDate;
    }

    /// 按钮高度
    const double _buttonHeight = 60.0;

    String okText = Custom_Localizations.current?.ok ?? _localizations.ok;

    String cancelText =
        Custom_Localizations.current?.cancel ?? _localizations.cancel;

    return SafeArea(
      child: Container(
        height: MediaQuery.of(context).size.height * 0.5,
        margin: const EdgeInsets.symmetric(
          horizontal: 10.0,
          vertical: 10.0,
        ),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: <Widget>[
            Flexible(
              child: Container(
                decoration: BoxDecoration(
                  color: Theme.of(context).canvasColor,
                  borderRadius: BorderRadius.vertical(
                    top: Radius.circular(12.0),
                  ),
                  shape: BoxShape.rectangle,
                ),
                child: CupertinoDatePicker(
                  mode: mode ?? CupertinoDatePickerMode.date,
                  initialDateTime: date,
                  minimumDate: minimumDate,
                  maximumDate: maximumDate,
                  use24hFormat: use24hFormat,
                  onDateTimeChanged: (val) {
                    date = val;
                  },
                ),
              ),
            ),
            Divider(indent: 0.0),
            Container(
              height: _buttonHeight,
              child: TextButton(
                style: ButtonStyle(
                  overlayColor:
                      WidgetStateProperty.all(Theme.of(context).splashColor),
                  backgroundColor:
                      WidgetStateProperty.all(Theme.of(context).canvasColor),
                  shape: WidgetStateProperty.resolveWith((states) {
                    return RoundedRectangleBorder(
                      borderRadius: BorderRadius.vertical(
                        bottom: Radius.circular(12.0),
                      ),
                    );
                  }),
                ),
                child: Text(
                  okText,
                  style: Theme.of(context).textTheme.labelLarge,
                ),
                onPressed: () {
                  Navigator.pop(context, date);
                },
              ),
            ),
            SizedBox(height: 10.0),
            Container(
              height: _buttonHeight,
              child: TextButton(
                style: ButtonStyle(
                  overlayColor:
                      WidgetStateProperty.all(Theme.of(context).splashColor),
                  backgroundColor:
                      WidgetStateProperty.all(Theme.of(context).canvasColor),
                  shape: WidgetStateProperty.all<OutlinedBorder>(
                    RoundedRectangleBorder(
                      borderRadius: BorderRadius.all(
                        Radius.circular(12.0),
                      ),
                    ),
                  ),
                ),
                child: Text(
                  cancelText,
                  style: Theme.of(context).textTheme.labelLarge,
                ),
                onPressed: () {
                  Navigator.pop(context, null);
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}
